/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jeffom
 */
public class Chat extends Thread{
    Socket s;
    ObjectInputStream in;
    Pacote pack;
    Interface janela;

    public Chat (Socket socket,Interface janela){
        s = socket;
        this.janela = janela;
        pack = new Pacote("","","");
        this.start();
    }
    
    public void getStream() throws IOException, ClassNotFoundException
    {
        in = new ObjectInputStream(s.getInputStream());        
        pack = (Pacote)in.readObject();        
        
        if(pack.getTipo().compareTo("chat") == 0 ){
            //print mensagem            
            janela.setTextBox(pack.getNome()+ ": " + pack.getMsg());
        }
    }
    
    public void run()
    {
        try {
            while(true){                
                in = new ObjectInputStream(s.getInputStream());
                pack = (Pacote)in.readObject();
                if (pack.getTipo().compareTo("chat") == 0) {                    
                janela.setTextBox(pack.getNome() + ": " + pack.getMsg());
                }
            }
        } catch (IOException ex) {
            try {
                in.close();
            } catch (IOException ex1) {
                Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex1);
            }
        } catch (ClassNotFoundException ex) {
            try {
                in.close();
            } catch (IOException ex1) {
                Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

}
